<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QNetworkDiskCache Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QNetworkDiskCache Class Reference<br /><sup><sup>[<a href="qtnetwork.html">QtNetwork</a> module]</sup></sup></h1><p>The QNetworkDiskCache class provides a very basic disk cache.
<a href="#details">More...</a></p>

<p>Inherits <a href="qabstractnetworkcache.html">QAbstractNetworkCache</a>.</p><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qnetworkdiskcache.html#QNetworkDiskCache">__init__</a></b> (<i>self</i>, QObject&#160;<i>parent</i>&#160;=&#160;None)</li><li><div class="fn" />QString <b><a href="qnetworkdiskcache.html#cacheDirectory">cacheDirectory</a></b> (<i>self</i>)</li><li><div class="fn" />int <b><a href="qnetworkdiskcache.html#cacheSize">cacheSize</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qnetworkdiskcache.html#clear">clear</a></b> (<i>self</i>)</li><li><div class="fn" />QIODevice <b><a href="qnetworkdiskcache.html#data">data</a></b> (<i>self</i>, QUrl&#160;<i>url</i>)</li><li><div class="fn" />int <b><a href="qnetworkdiskcache.html#expire">expire</a></b> (<i>self</i>)</li><li><div class="fn" />QNetworkCacheMetaData <b><a href="qnetworkdiskcache.html#fileMetaData">fileMetaData</a></b> (<i>self</i>, QString&#160;<i>fileName</i>)</li><li><div class="fn" /><b><a href="qnetworkdiskcache.html#insert">insert</a></b> (<i>self</i>, QIODevice&#160;<i>device</i>)</li><li><div class="fn" />int <b><a href="qnetworkdiskcache.html#maximumCacheSize">maximumCacheSize</a></b> (<i>self</i>)</li><li><div class="fn" />QNetworkCacheMetaData <b><a href="qnetworkdiskcache.html#metaData">metaData</a></b> (<i>self</i>, QUrl&#160;<i>url</i>)</li><li><div class="fn" />QIODevice <b><a href="qnetworkdiskcache.html#prepare">prepare</a></b> (<i>self</i>, QNetworkCacheMetaData&#160;<i>metaData</i>)</li><li><div class="fn" />bool <b><a href="qnetworkdiskcache.html#remove">remove</a></b> (<i>self</i>, QUrl&#160;<i>url</i>)</li><li><div class="fn" /><b><a href="qnetworkdiskcache.html#setCacheDirectory">setCacheDirectory</a></b> (<i>self</i>, QString&#160;<i>cacheDir</i>)</li><li><div class="fn" /><b><a href="qnetworkdiskcache.html#setMaximumCacheSize">setMaximumCacheSize</a></b> (<i>self</i>, int&#160;<i>size</i>)</li><li><div class="fn" /><b><a href="qnetworkdiskcache.html#updateMetaData">updateMetaData</a></b> (<i>self</i>, QNetworkCacheMetaData&#160;<i>metaData</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QNetworkDiskCache class provides a very basic disk
cache.</p>
<p>QNetworkDiskCache stores each url in its own file inside of the
cacheDirectory using <a href="qdatastream.html">QDataStream</a>.
Files with a text MimeType are compressed using qCompress. Each
cache file starts with "<a href="qmake-environment-reference.html#cache">cache_</a>" and ends in
".cache". Data is written to disk only in <a href="qnetworkdiskcache.html#insert">insert</a>() and <a href="qnetworkdiskcache.html#updateMetaData">updateMetaData</a>().</p>
<p>Currently you can not share the same cache files with more then
one disk cache.</p>
<p>QNetworkDiskCache by default limits the amount of space that the
cache will use on the system to 50MB.</p>
<p>Note you have to set the cache directory before it will
work.</p>
<p>A network disk cache can be enabled by:</p>
<pre class="cpp">
 <span class="type"><a href="qnetworkaccessmanager.html">QNetworkAccessManager</a></span> <span class="operator">*</span>manager <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qnetworkaccessmanager.html">QNetworkAccessManager</a></span>(<span class="keyword">this</span>);
 <span class="type">QNetworkDiskCache</span> <span class="operator">*</span>diskCache <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QNetworkDiskCache</span>(<span class="keyword">this</span>);
 diskCache<span class="operator">-</span><span class="operator">&gt;</span><a href="qnetworkdiskcache.html#setCacheDirectory">setCacheDirectory</a>(<span class="string">"cacheDir"</span>);
 manager<span class="operator">-</span><span class="operator">&gt;</span>setCache(diskCache);
</pre>
<p>When sending requests, to control the preference of when to use
the cache and when to use the network, consider the following:</p>
<pre class="cpp">
 <span class="comment">// do a normal request (preferred from network, as this is the default)</span>
 <span class="type"><a href="qnetworkrequest.html">QNetworkRequest</a></span> request(<span class="type"><a href="qurl.html">QUrl</a></span>(<span class="type"><a href="qstring.html">QString</a></span>(<span class="string">"http://qt.nokia.com"</span>)));
 manager<span class="operator">-</span><span class="operator">&gt;</span>get(request);

 <span class="comment">// do a request preferred from cache</span>
 <span class="type"><a href="qnetworkrequest.html">QNetworkRequest</a></span> request2(<span class="type"><a href="qurl.html">QUrl</a></span>(<span class="type"><a href="qstring.html">QString</a></span>(<span class="string">"http://qt.nokia.com"</span>)));
 request2<span class="operator">.</span>setAttribute(<span class="type"><a href="qnetworkrequest.html">QNetworkRequest</a></span><span class="operator">.</span>CacheLoadControlAttribute<span class="operator">,</span> <span class="type"><a href="qnetworkrequest.html">QNetworkRequest</a></span><span class="operator">.</span>PreferCache);
 manager<span class="operator">-</span><span class="operator">&gt;</span>get(request2);
</pre>
<p>To check whether the response came from the cache or from the
network, the following can be applied:</p>
<pre class="cpp">
 <span class="type">void</span> replyFinished(<span class="type"><a href="qnetworkreply.html">QNetworkReply</a></span> <span class="operator">*</span>reply) {
     <span class="type"><a href="qvariant.html">QVariant</a></span> fromCache <span class="operator">=</span> reply<span class="operator">-</span><span class="operator">&gt;</span>attribute(<span class="type"><a href="qnetworkrequest.html">QNetworkRequest</a></span><span class="operator">.</span>SourceIsFromCacheAttribute);
     <a href="qtcore.html#qDebug">qDebug</a>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">"page from cache?"</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> fromCache<span class="operator">.</span>toBool();
 }
</pre><hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QNetworkDiskCache" />QNetworkDiskCache.__init__ (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>parent</i>&#160;=&#160;None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Creates a new disk cache. The <i>parent</i> argument is passed
to <a href="qabstractnetworkcache.html">QAbstractNetworkCache</a>'s
constructor.</p>


<h3 class="fn"><a name="cacheDirectory" />QString QNetworkDiskCache.cacheDirectory (<i>self</i>)</h3><p>Returns the location where cached files will be stored.</p>
<p><b>See also</b> <a href="qnetworkdiskcache.html#setCacheDirectory">setCacheDirectory</a>().</p>


<h3 class="fn"><a name="cacheSize" />int QNetworkDiskCache.cacheSize (<i>self</i>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#cacheSize">QAbstractNetworkCache.cacheSize</a>().</p>


<h3 class="fn"><a name="clear" />QNetworkDiskCache.clear (<i>self</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>void clear()</tt>.</p><p>Reimplemented from <a href="qabstractnetworkcache.html#clear">QAbstractNetworkCache.clear</a>().</p>


<h3 class="fn"><a name="data" /><a href="qiodevice.html">QIODevice</a> QNetworkDiskCache.data (<i>self</i>, <a href="qurl.html">QUrl</a>&#160;<i>url</i>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#data">QAbstractNetworkCache.data</a>().</p>


<h3 class="fn"><a name="expire" />int QNetworkDiskCache.expire (<i>self</i>)</h3><p>Cleans the cache so that its size is under the maximum cache
size. Returns the current size of the cache.</p>
<p>When the current size of the cache is greater than the <a href="qnetworkdiskcache.html#maximumCacheSize">maximumCacheSize</a>()
older cache files are removed until the total size is less then 90%
of <a href="qnetworkdiskcache.html#maximumCacheSize">maximumCacheSize</a>()
starting with the oldest ones first using the file creation date to
determine how old a cache file is.</p>
<p>Subclasses can reimplement this function to change the order
that cache files are removed taking into account information in the
application knows about that <a href="qnetworkdiskcache.html">QNetworkDiskCache</a> does not, for
example the number of times a cache is accessed.</p>
<p>Note: <a href="qnetworkdiskcache.html#cacheSize">cacheSize</a>()
calls expire if the current cache size is unknown.</p>
<p><b>See also</b> <a href="qnetworkdiskcache.html#maximumCacheSize">maximumCacheSize</a>()
and <a href="qnetworkdiskcache.html#fileMetaData">fileMetaData</a>().</p>


<h3 class="fn"><a name="fileMetaData" /><a href="qnetworkcachemetadata.html">QNetworkCacheMetaData</a> QNetworkDiskCache.fileMetaData (<i>self</i>, QString&#160;<i>fileName</i>)</h3><p>Returns the <a href="qnetworkcachemetadata.html">QNetworkCacheMetaData</a> for the
cache file <i>fileName</i>.</p>
<p>If <i>fileName</i> is not a cache file <a href="qnetworkcachemetadata.html">QNetworkCacheMetaData</a> will be
invalid.</p>


<h3 class="fn"><a name="insert" />QNetworkDiskCache.insert (<i>self</i>, <a href="qiodevice.html">QIODevice</a>&#160;<i>device</i>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#insert">QAbstractNetworkCache.insert</a>().</p>


<h3 class="fn"><a name="maximumCacheSize" />int QNetworkDiskCache.maximumCacheSize (<i>self</i>)</h3><p>Returns the current maximum size in bytes for the disk
cache.</p>
<p><b>See also</b> <a href="qnetworkdiskcache.html#setMaximumCacheSize">setMaximumCacheSize</a>().</p>


<h3 class="fn"><a name="metaData" /><a href="qnetworkcachemetadata.html">QNetworkCacheMetaData</a> QNetworkDiskCache.metaData (<i>self</i>, <a href="qurl.html">QUrl</a>&#160;<i>url</i>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#metaData">QAbstractNetworkCache.metaData</a>().</p>


<h3 class="fn"><a name="prepare" /><a href="qiodevice.html">QIODevice</a> QNetworkDiskCache.prepare (<i>self</i>, <a href="qnetworkcachemetadata.html">QNetworkCacheMetaData</a>&#160;<i>metaData</i>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#prepare">QAbstractNetworkCache.prepare</a>().</p>


<h3 class="fn"><a name="remove" />bool QNetworkDiskCache.remove (<i>self</i>, <a href="qurl.html">QUrl</a>&#160;<i>url</i>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#remove">QAbstractNetworkCache.remove</a>().</p>


<h3 class="fn"><a name="setCacheDirectory" />QNetworkDiskCache.setCacheDirectory (<i>self</i>, QString&#160;<i>cacheDir</i>)</h3><p>Sets the directory where cached files will be stored to
<i>cacheDir</i></p>
<p><a href="qnetworkdiskcache.html">QNetworkDiskCache</a> will
create this directory if it does not exists.</p>
<p>Prepared cache items will be stored in the new cache directory
when they are inserted.</p>
<p><b>See also</b> <a href="qnetworkdiskcache.html#cacheDirectory">cacheDirectory</a>() and
<a href="qdesktopservices.html#StandardLocation-enum">QDesktopServices.CacheLocation</a>.</p>


<h3 class="fn"><a name="setMaximumCacheSize" />QNetworkDiskCache.setMaximumCacheSize (<i>self</i>, int&#160;<i>size</i>)</h3><p>Sets the maximum size of the disk cache to be <i>size</i> in
bytes.</p>
<p>If the new size is smaller then the current cache size then the
cache will call <a href="qnetworkdiskcache.html#expire">expire</a>().</p>
<p><b>See also</b> <a href="qnetworkdiskcache.html#maximumCacheSize">maximumCacheSize</a>().</p>


<h3 class="fn"><a name="updateMetaData" />QNetworkDiskCache.updateMetaData (<i>self</i>, <a href="qnetworkcachemetadata.html">QNetworkCacheMetaData</a>&#160;<i>metaData</i>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#updateMetaData">QAbstractNetworkCache.updateMetaData</a>().</p>
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;4.9.1 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2011</td><td align="right" width="25%">Qt&#160;4.8.0</td></tr></table></div></address></body></html>